<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

/**
 * 渠道来源管理
 *
 * @author Xmh
 */
class Channel {

    private $ComeFrom;
    private $from_id = '';

    public function __construct($from_id) {
        $this->ComeFrom = M("come_from");
        $this->from_id = $from_id;
    }

    /**
     * 获得渠道来源列表
     */
    public function get_channel_list($cols = '*') {
        // $where['from_id'] = array('neq',5);
        $where['is_delete'] = array('eq',0);
        return $this->ComeFrom->field($cols)->where($where)->select();
    }

    /**
     * 获取父级渠道来源
     */
    public function get_parent_channel_list() {
        $pfrom = M("come_from")->where("pid=-2 AND is_delete=0")->select();
        return $pfrom;
    }


    /**
     * 获取子级渠道来源
     */
    public function get_child_channel_list() {
        $cols = 'a.from_id,a.from_name,b.from_name AS pname,b.from_id AS pid,b.sort AS psort';
        $condition['a.pid'] = array('neq', '-2');
        $condition['a.is_delete'] = array('eq', '0');
        // $condition['a.from_id'] = array('neq', '-1');
        $order = 'psort ASC,from_id ASC,pid DESC';
        $from = M("come_from")->alias('a')
                ->join('INNER JOIN (SELECT from_id,from_name,sort FROM au_come_from WHERE pid=-2 AND is_delete=0) AS b ON a.pid=b.from_id')
                ->field($cols)
                ->where($condition)
                ->order($order)
                ->select();
        return $from;
    }

    /**
     * 获取渠道来源信息
     */
    public static function get_from() {
        $from = M("come_from a")->field("a.from_id,a.from_name,b.from_name AS pname,b.from_id AS pid,b.sort AS psort")
                ->join('(SELECT from_id,from_name,sort FROM au_come_from WHERE pid=-2 AND is_delete=0) AS b ON a.pid=b.from_id')
                ->where("a.pid!=-2 AND a.is_delete=0")
                ->order("psort ASC,pid DESC")
                ->select();
        $show_from = array();
        foreach ($from as $key => $val) {
            $show_from[$val['from_id']] = $val['pname'] . '-' . $val['from_name'];
        }
        return $show_from;
    }

    /**
     * 按父ID获得子渠道
     */
    public function get_children_channel() {
        $map['pid'] = $this->from_id;
        return $this->ComeFrom->where($map)->select();
    }
    /**
     * 获取子渠道的from_id集合
     */
    public function get_children_channel_from_id_list() {
        $map['pid'] = $this->from_id;
        $list = $this->ComeFrom->where($map)->select();
        foreach($list as $k=>$v){
            $from_id_list[$k] = $v['from_id'];
        }
        return $from_id_list;
    }
    /**
     * 获取渠道来源详情
     */
    public function get_channel($from_id=0){
        return $this->ComeFrom->find($from_id);
    }
    /**
     * 根据父渠道名称获取渠道详情
     * @param type $parent_name
     * @return type
     */
    public function getChannelDetailByParentName($parent_name) {
        $map['from_name'] = $parent_name;
        $map['pid'] = array('eq','-2');
        return $this->ComeFrom->where($map)->find();
    }
    /**
     * 根据渠道名称获取渠道详情
     * @param type $from_name
     * @return type
     */
    public function getChannelDetailByName($from_name) {
        $map['from_name'] = $from_name;
        $map['pid'] = array('neq','-2');
        return $this->ComeFrom->where($map)->find();
    }
    /**
     * 根据ID获取渠道详情
     * @param type $from_name
     * @return type
     */
    public function getChannelByFromId($pid) {
        $map['from_id'] = $pid;
        return $this->ComeFrom->where($map)->find();
    }

    /**
     * 添加渠道来源
     */
    public function add_channel($data) {
        $data['createtime'] = date("Y-m-d H:i:s");
        return $this->ComeFrom->add($data);
    }

    /**
     * 编辑渠道来源
     */
    public function edit_channel($data) {
        return $this->ComeFrom->save($data);
    }

    /**
     * 删除渠道来源
     */
    public function del_channel() {
        $map['from_id'] = $this->from_id;
        return $this->ComeFrom->where($map)->delete();
    }

    /**
     * 获取车辆来源全名称
     * @param $from_id
     * @return bool
     */
    public static function get_full_name_by_id($from_id)
    {
        $prefix = C("DB_PREFIX");
        $comefrom = M("come_from")->alias('a')
	        ->field("a.from_name,b.from_name as parent_name")
            ->join("LEFT JOIN ".$prefix."come_from as b ON a.pid=b.from_id")
            ->where("a.from_id=".$from_id)
	        ->find();

        return $comefrom['parent_name'] ."-" .$comefrom['from_name'];
    }


    /**
     * 获取渠道来源信息
     */
    public static function get_comefrom_list()
    {
        $from = M('come_from')->field('from_id,pid,from_name')
            ->where('pid=-2 AND is_delete=0')
            ->select();
        $show_from = array();
        foreach ($from as $key => $val) {
            $child_from = M("come_from a")->field("a.from_id,a.from_name,b.sort AS psort")
                ->join('(SELECT from_id,from_name,sort FROM au_come_from WHERE pid=-2 AND is_delete=0) AS b ON a.pid=b.from_id')
//                ->join('LEFT JOIN '.C("DB_PREFIX").'fours f ON a.from_id = f.channel_id')
                ->where("a.pid!=-2 AND a.is_delete=0")
                ->where("a.pid=".$val['from_id'])
                ->order("psort ASC,from_id ASC,pid DESC")
                ->select();
            $show_from[$val['from_id']] = $child_from;
        }
        return $show_from;
    }

    /**
     * 获取城市对应的4s店列表
     * @param array $bc_city
     *
     * @return array
     */
    public static function get_city_fours_list($bc_city = array())
    {
        $city_fours_list = array();
        foreach ($bc_city as $kb => $vb) {
            $city_fours = M("come_from")->alias('a')->field('a.from_id,a.from_name')
                ->join(C('DB_PREFIX') . 'fours as f ON a.from_id = f.channel_id')
                ->where(array('f.city' => $kb))
                ->order('from_id ASC,pid DESC')
                ->select();
            $city_fours_list[$kb] = $city_fours;
        }
        return $city_fours_list;
    }
}